<template>
  <div class="table-insert-modal" v-if="visible" @click.self="closeModal">
    <div class="modal-content">
      <div class="modal-header">
        <h3>插入表格</h3>
        <button class="close-btn" @click="closeModal">×</button>
      </div>
      
      <div class="modal-body">
        <div class="table-size-selector">
          <div class="size-inputs">
            <div class="input-group">
              <label>行数:</label>
              <input 
                type="number" 
                v-model.number="rows" 
                min="1" 
                max="10"
                class="size-input"
              >
            </div>
            <div class="input-group">
              <label>列数:</label>
              <input 
                type="number" 
                v-model.number="cols" 
                min="1" 
                max="10"
                class="size-input"
              >
            </div>
          </div>
          
          <div class="table-options">
            <div class="option-group">
              <label>表格宽度:</label>
              <select v-model="tableWidth" class="width-select">
                <option value="100%">100% (全宽)</option>
                <option value="80%">80%</option>
                <option value="60%">60%</option>
                <option value="auto">自动</option>
              </select>
            </div>
            <div class="option-group">
              <label>单元格宽度:</label>
              <select v-model="cellWidth" class="width-select">
                <option value="auto">自动</option>
                <option value="120px">120px</option>
                <option value="150px">150px</option>
                <option value="200px">200px</option>
              </select>
            </div>
          </div>
          
          <div class="table-preview">
            <div class="preview-label">预览:</div>
            <div class="preview-table">
              <table>
                <tbody>
                  <tr v-for="row in rows" :key="row">
                    <td v-for="col in cols" :key="col"></td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      
      <div class="modal-footer">
        <button class="btn btn-secondary" @click="closeModal">取消</button>
        <button class="btn btn-primary" @click="insertTable">插入表格</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TableInsert',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      rows: 3,
      cols: 3,
      tableWidth: '100%',
      cellWidth: 'auto'
    }
  },
  methods: {
    insertTable() {
      const tableHTML = this.generateTableHTML()
      this.$emit('insert-table', tableHTML)
      this.closeModal()
    },
    
    generateTableHTML() {
      const widthStyle = this.tableWidth === 'auto' ? '' : `width: ${this.tableWidth};`
      const cellWidthStyle = this.cellWidth === 'auto' ? '' : `width: ${this.cellWidth};`
      
      let html = `<table style="border-collapse: collapse; ${widthStyle} margin: 8px 0; table-layout: fixed;">`
      
      for (let i = 0; i < this.rows; i++) {
        html += '<tr>'
        for (let j = 0; j < this.cols; j++) {
          html += `<td style="border: 1px solid #ddd; padding: 8px; text-align: left; ${cellWidthStyle} word-wrap: break-word; overflow-wrap: break-word;">&nbsp;</td>`
        }
        html += '</tr>'
      }
      
      html += '</table>'
      return html
    },
    
    closeModal() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>
.table-insert-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  width: 450px;
  max-width: 90vw;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #e5e7eb;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #111827;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: #6b7280;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s;
}

.close-btn:hover {
  background: #f3f4f6;
  color: #374151;
}

.modal-body {
  padding: 20px;
}

.size-inputs {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-group label {
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}

.size-input {
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 14px;
  width: 80px;
}

.size-input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.table-options {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  padding: 16px;
  background: #f9fafb;
  border-radius: 6px;
  border: 1px solid #e5e7eb;
}

.option-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.option-group label {
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}

.width-select {
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 14px;
  background: white;
  cursor: pointer;
}

.width-select:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.table-preview {
  border-top: 1px solid #e5e7eb;
  padding-top: 20px;
}

.preview-label {
  font-weight: 500;
  color: #374151;
  margin-bottom: 12px;
  font-size: 14px;
}

.preview-table {
  border: 1px solid #d1d5db;
  border-radius: 4px;
  overflow: hidden;
  background: white;
  display: inline-block;
}

.preview-table table {
  border-collapse: collapse;
  table-layout: fixed;
}

.preview-table td {
  border: 1px solid #e5e7eb;
  padding: 4px;
  width: 20px;
  height: 20px;
  background: #f9fafb;
  box-sizing: border-box;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid #e5e7eb;
  background: #f9fafb;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary {
  background: #f3f4f6;
  color: #374151;
}

.btn-secondary:hover {
  background: #e5e7eb;
}

.btn-primary {
  background: #3b82f6;
  color: white;
}

.btn-primary:hover {
  background: #2563eb;
}
</style> 